package com.zhen.util;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.mail.smtp.SMTPTransport;

public class Checked extends SMTPTransport {

	private static final Logger log = LoggerFactory.getLogger(Checked.class);
	public Checked(Session session, URLName urlname) {
		super(session, urlname);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(Checked.check());
		/*try {
			// URLName url=new
			// URLName("smtp","mail.gtexpress.cn",25,"","*@gtexpress.cn","*");
			// URLName url=new
			// URLName("smtp","smtp.163.com",25,"","*@163.com","*");
			// URLName url=new
			// URLName("smtp","smtp.qq.com",25,"","*@qq.com","");
			URLName url = new URLName("smtp", "smtp.qq.com", 465, "", "623037142@qq.com", "sreiivimeyspbdgf");
			Session session = Session.getInstance(new Properties());
			Checked login = new Checked(session, url);
			System.out.println("登陆:" + login.login(url));
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		System.out.println(check());
	}
	public static boolean check(){
		/*URLName url = new URLName("smtp", "smtp.qq.com", 25, "", "623037142@qq.com", "sreiivimeyspbdgf");
		Session session = Session.getInstance(new Properties());
		Checked login = new Checked(session, url);
		return login.login(url);*/
		//1、连接邮件服务器的参数配置
       Properties props = new Properties();
       //设置用户的认证方式
       props.setProperty("mail.smtp.auth", "true");
       //设置传输协议
       props.setProperty("mail.transport.protocol", "smtp");
       //设置发件人的SMTP服务器地址
       props.setProperty("mail.smtp.host", "smtp.qq.com");
       props.setProperty("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
//	       props.setProperty("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
       //2、创建定义整个应用程序所需的环境信息的 Session 对象
       Session session = Session.getInstance(props);
       //设置调试信息在控制台打印出来
//	       session.setDebug(true);
       //3、创建邮件的实例对象
//	       Message msg = getMimeMessage(session,"");
       //4、根据session对象获取邮件传输对象Transport
       //设置发件人的账户名和密码
       Transport transport = null;
       try {
	    	transport = session.getTransport();
			transport.connect("623037142@qq.com", "xvktyojnpcbmbdia");
			return true;
		} catch (MessagingException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return false;
			
		}finally {
			if(transport!=null){
				try {
					transport.close();
				} catch (MessagingException e) {
					e.printStackTrace();
					log.error(e.getMessage());
				}
			}
		}
	}

	public boolean login(URLName url) {
		
		
		boolean login = false;
		try {
			if ("".equals(url.getPassword()) || "".equals(url.getHost()) || "".equals(url.getPort())
					|| "".equals(url.getUsername())) {
				return false;
			}
			login = this.protocolConnect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword());
		} catch (MessagingException e) {
			return false;
		}
		return login;
	}

}
